<?php
    if(isset($_POST['setRecipient'])):
        if(isset($_POST['user'])) {
            $_SESSION['user_list'] = array();
            $iter=0;
            foreach($_POST['user'] as $user):
                $_SESSION['user_list'][$iter] = $user;
                $iter++;
            endforeach;
        } else {
            if(isset($_SESSION['user_list'])) {
                $this->session->remove('user_list');
            }
        }
?>
    <script language="javascript">
        self.close();
        window.opener.location.reload();
    </script>
<?php endif ?>

<h1>Choose Recipient</h1>
<form action="" method="post" name="recipient">
    <?php foreach($recipients as $recipient): 
            if($recipient["User"]["id"] != $this->session->get('user_id')):
                $checked=0;
                if(isset($_SESSION['user_list'])):
                    foreach ($this->session->get('user_list') as $user):
                        if($recipient["User"]["name"] == $user):
                            $checked=1;
                        endif;
                    endforeach;
                endif;
                if($checked==1) {
    ?>
        <input checked="true" type="checkbox" name="user[]" value="<?php echo $recipient["User"]["name"] ?>"><?php echo $recipient["User"]["name"] ?><br/>
    <?php } else { ?>
        <input type="checkbox" name="user[]" value="<?php echo $recipient["User"]["name"] ?>"><?php echo $recipient["User"]["name"] ?><br/>
    <?php }
            endif;
        endforeach;
    ?>
        <input type="submit" value="Submit" name="setRecipient" />
</form>